package kuzmenkov.oip.controller;

import kuzmenkov.oip.common.LinkValidator;
import kuzmenkov.oip.service.CommentService;
import kuzmenkov.oip.service.impl.Chunk;
import kuzmenkov.oip.service.impl.CommentItem;
import org.springframework.web.jsf.FacesContextUtils;

import javax.faces.component.html.HtmlCommandLink;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import java.util.List;
import java.util.logging.Logger;

/**
 * This is a backing bean of a comment page
 * User: Michael Kuzmenkov
 * Date: 27.03.2009
 * Time: 11:47:44
 */
public class CommentController extends ErrorSupport {
    private static final Logger log = Logger.getLogger(CommentController.class.getName());

    private CommentService service;

    private String userName;
    private String email;
    private String message;


    public CommentService getService() {
        return service;
    }

    public CommentController() {
        try {
            service = (CommentService) FacesContextUtils.
                    getWebApplicationContext(FacesContext.getCurrentInstance()).
                    getBean("commentService");
        } catch (Exception e) {
            addError(e);
        }
    }

    public int getChunkIndex () {
        return service.getCurrentChunkIndex();
    }


    public List<CommentItem> getComments() {
        List<CommentItem> cs = null;
        try {
            cs = service.getCommentItems();
        } catch (Exception e) {
            addError(e);
        }
        return cs;
    }

    public String sendComment() {
        try {

            if (!LinkValidator.validateEmailId(email)) {
                String err = getI18nTool().getMessage("CommentController.emailIsNotValid", new Object[]{email});
                throw new IllegalStateException(err);
            }

            service.storeComment(userName.trim(), email.trim(), message.trim());
            message = null;
        } catch (Exception e) {
            addError(e);
            return "failure";
        }
        return "success";
    }

    public String getModuleName() {
        String n = null;
        try {
            n = service.getModuleName();
        } catch (Exception e) {
            addError(e);
        }
        return n;
    }


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<Chunk> getChunks() {
        List<Chunk> chunks = null;
        try {
            chunks = service.getChunks();
        } catch (Exception e) {
            addError(e);
        }
        return chunks;
    }

    public void chunking(ActionEvent event) {
        try {
            Object comp = event.getSource();
            HtmlCommandLink link = (HtmlCommandLink) comp;

            Integer number = null;

            number = (Integer) link.getValue();
            int num = number.intValue() - 1;
            service.chunking(num);
        } catch (Exception e) {
            addError(e);
        }

    }
}